﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Infragistics.Windows.DataPresenter;


namespace ReclutamientoCosea
{
	/// <summary>
	/// Interaction logic for usrEntrevista.xaml
	/// </summary>
	public partial class usrEntrevista : UserControl
	{
        private static AppSettingsReader settings = new AppSettingsReader();
        public event CerroVentana SeCerroVentana;
        private System.Windows.Forms.Timer timerTrack;

        private string idCandidato = "";

        public string IdCandidato
        {
            get { return idCandidato; }
            set { idCandidato = value; }
        }
        private string idUsuario = "";

        public string IdUsuario 
        {
            get { return idUsuario; }
            set { idUsuario = value; }
        }

		public usrEntrevista()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(usrEntrevista_Loaded);
		}

        void usrEntrevista_Loaded(object sender, RoutedEventArgs e)
        {
            CargaEntrevistas();
            txtTexto.IsEnabled = false;
            btnGuardar.IsEnabled = false;

            //timerTrack = new System.Windows.Forms.Timer();
            //timerTrack.Interval = 2500;
            //timerTrack.Tick += new EventHandler(timerTrack_Tick);
            //timerTrack.Enabled = true;
        }

        void timerTrack_Tick(object sender, EventArgs e)
        {
            
        }

        private void CargaEntrevistas()
        {
            bdSQLServer bd = new bdSQLServer((string)settings.GetValue("cadenaconexion", typeof(string)));
            string strSQL = "";
            try
            {
                strSQL += "select comentario,fecha ";
                strSQL += "from entrevista ";
                strSQL += "where idcandidato = '{0}' ";                
                strSQL += "order by fecha desc ";


                bd.bdConecta();
                string query = string.Format(strSQL, idCandidato);
                DataSet ds = bd.dataSetConsulta(query, "entre");
                dgEntrevistas.DataSource = ds.Tables[0].DefaultView;

            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }

            finally
            {
                bd.bdCierra();
            }
        }

        private void guardaEntrevista()
        {
            bdSQLServer bd = new bdSQLServer((string)settings.GetValue("cadenaconexion", typeof(string)));
            string strSQL = "exec SPInsEntrevista '{0}','{1}','{2}'";
            try
            {
                strSQL = string.Format(strSQL, idCandidato, idUsuario, Utilerias.ConvertRichTextBoxContentsToString(txtTexto));
                bd.bdConecta();
                int iEjecuta = bd.bdEjecuta(strSQL);                

            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }

            finally
            {
                bd.bdCierra();
                CargaEntrevistas();
                txtTexto.Document.Blocks.Clear();
                txtTexto.IsEnabled = false;
                btnGuardar.IsEnabled = false;
            }

        }

		private void btnGuardar_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            guardaEntrevista();
            //try
            //{
            //    SqlConnection cnn = new SqlConnection((string)settings.GetValue("cadenaconexion", typeof(string)));
            //    cnn.Open();
            //    SqlCommand cmd = new SqlCommand();
            //    cmd = new SqlCommand("SPInsEntrevista", cnn);

            //    cmd.CommandType = CommandType.StoredProcedure;
            //    SqlParameter sqlidcandidato = new SqlParameter("@idcandidato", idCandidato);
            //    SqlParameter sqlidusuario = new SqlParameter("@idusuario", idUsuario);
            //    SqlParameter sqlcomentarios = new SqlParameter("@comentario", SqlDbType.NChar);
            //    cmd.Parameters.Add(sqlidcandidato);
            //    cmd.Parameters.Add(sqlidusuario);
            //    sqlcomentarios.Value = Utilerias.ConvertRichTextBoxContentsToString(txtTexto);
            //    cmd.Parameters.Add(sqlcomentarios);
            //    cmd.ExecuteNonQuery();
            //    cmd.Dispose();
            //    cmd = null;
            //    cnn.Close();
            //    cnn = null;

            //}
            //catch (Exception ex)
            //{
            //    wExcepcionesSistema wError = new wExcepcionesSistema();
            //    wError.txtExcepcion.Text = ex.Message;
            //    wError.ShowDialog();
            //}
            //finally
            //{
            //    CargaEntrevistas();
            //    txtTexto.Document.Blocks.Clear();
            //    txtTexto.IsEnabled = false;
            //    btnGuardar.IsEnabled = false;
            //}
		}

		private void btnSalir_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            SeCerroVentana(true);
		}

        private void dgEntrevistas_RecordActivated(object sender, Infragistics.Windows.DataPresenter.Events.RecordActivatedEventArgs e)
        {
            if (e.Record is DataRecord)
            {
                // Cast the record passed in as a DataRecord
                DataRecord myRecord = (DataRecord)e.Record;
                FlowDocument mcFlowDoc = new FlowDocument();
                Paragraph para = new Paragraph();
                para.Inlines.Add(new Run(myRecord.Cells[0].Value.ToString()));
                mcFlowDoc.Blocks.Add(para);
                txtTexto.Document = mcFlowDoc;
                txtTexto.IsEnabled = false;
                btnGuardar.IsEnabled = false;
            }
        }

        private void dgEntrevistas_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }

        private void btnNuevo_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            txtTexto.IsEnabled = true;
            btnGuardar.IsEnabled = true;
            txtTexto.Document.Blocks.Clear();
        }
	}
}